/**
 * Created by Kpeng on 2017/7/10.
 * Depending on the jquery-1.11.3.min.js.
 * Depending on the style.css.
 * To cite this document, always state the source as shown above.
 */

$(function () {
    wzERP.BusinessConfig.init();
});

wzERP.BusinessConfig.init = function () {
    wzERP.BusinessConfig.addHandle(); //添加公司
    wzERP.BusinessConfig.editHandle(); //编辑公司
    wzERP.BusinessConfig.delHandle(); //删除公司
    wzERP.Common.checkBoxVal('.guanfei');
    wzERP.BusinessConfig.turnPage();//分页
    
};

//添加业务配置
wzERP.BusinessConfig.addHandle = function () {
    var opt = {
        ele: '.addBtn',
        modalBox: '#modal',
        modalTitleStr: '添加业务',
        onApprove: function () {
            if(!!wzERP.BusinessConfig.checkform('#business')){
                layer.msg('填写项不符合规则', {icon: 2});
                return false;
            }
            var name = $("#business").find("[name='businessName']").val();
            var flag = wzERP.Common.isExistTypeHandle("erp_business"," business_name='"+name+"'");
            if(flag)
            {
                layer.msg('业务名称已存在', {icon: 2});
                return false;
            }
            var name = $("#business").find("[name='businessCode']").val();
            var flag = wzERP.Common.isExistTypeHandle("erp_business"," business_code='"+name+"'");
            if(flag)
            {
                layer.msg('业务编号已存在', {icon: 2});
                return false;
            }
            wzERP.Common.saveHandle('#business', '#submitBusinessUrl', $(this).find('.setId'), null, wzERP.Common.delayReload(wzERP.delayTime));
        },
        snippets: $("#addBusinessHtml").val()
    };
    wzERP.Common.handleData(opt);
};

//表单验证出口
wzERP.BusinessConfig.checkform = function (ele) {
    /*Arg Guide
     @ ele 要验证的表单
     */
    $(ele).form('submit');
    return $(ele).hasClass('error');
}

//编辑业务配置
wzERP.BusinessConfig.editHandle = function () {
    var opt = {
        ele: '.editBtn',
        modalBox: '#modal',
        modalTitleStr: '编辑业务配置',
        onApprove: function () {
            if(!!wzERP.BusinessConfig.checkform('#business')){
                layer.msg('填写项不符合规则', {icon: 2});
                return false;
            }
            var org = $("#business").find("[name='businessName']")[0].defaultValue;
            var name = $("#business").find("[name='businessName']").val();
            if(org != name)
            {
                var flag = wzERP.Common.isExistTypeHandle("erp_business"," business_name='"+name+"'");
                if(flag)
                {
                    layer.msg('业务名称已存在', {icon: 2});
                    return false;
                }
            }
            org = $("#business").find("[name='businessCode']")[0].defaultValue;
            name = $("#business").find("[name='businessCode']").val();
            if(org != name)
            {
                var flag = wzERP.Common.isExistTypeHandle("erp_business"," business_code='"+name+"'");
                if(flag)
                {
                    layer.msg('业务编号已存在', {icon: 2});
                    return false;
                }
            }
            wzERP.Common.saveHandle('#business', '#updateBusinessUrl', $(this).find('.setId'), null, wzERP.Common.delayReload(wzERP.delayTime));
        },
        snippets: $("#businessFindHtml").val()
    };
    wzERP.Common.handleData(opt);
};

//删除业务配置
wzERP.BusinessConfig.delHandle = function () {
    /*var opt = {
        ele: '.delBtn',
        modalBox: '#modal',
        modalTitleStr: '',
        onApprove: function () {
            wzERP.Common.deleteData('#delBusinessUrl', $(this.modalBox).find('.setId'));
        },
        snippets: $("#businessDeleteHtml").val()
    };
    wzERP.Common.handleData(opt);*/
    
    
    $('#main').on('click', '.delBtn', function () {
        var $self = $(this);
        layer.alert('确定删除该业务吗？', {
                title: '提示信息',
                btn: ['取消', '确认'],
                btn1:function(index){
                    layer.close(index)
                },
                btn2:function(){
                    wzERP.Common.deleteData('#delBusinessUrl', $self)
                }
            }
        );
    })
    
    
};

//官费
wzERP.BusinessConfig.checkboxVal = function () {
    $(document).on('click', ".guanfei-hook", function () {
        this.value = this.checked ? 0 : 1;
    });
}

//分页跳转
wzERP.BusinessConfig.turnPage = function () {
    var opt = {
        pageUrl: $("#businessQueryPage").val(),
        pageSearch: "#searchForm",
        pagesTotal: parseInt($('#pages').val()),
        pageCurr: parseInt($('#current').val()),
        pageSize: parseInt($('#pageSize').val()),
        currId: "#current"
    };
    wzERP.Common.pagingHandle(opt);
};

//
function search(){
    $("#current").val(1);
    $("#searchForm").action="/basedata/business/queryPage";
    $("#searchForm").submit();
}

//表单验证器
wzERP.BusinessConfig.verifier = function (_selector) {
    /*Arg Guide
     @_selector: 对象选择器
     */
    $(_selector).form({
        on: 'blur',
        // inline: true,
        transition: 'fade',
        fields: {
            //业务名称
            businessName: {
                identifier: 'businessName',
                rules: [{
                    type: 'empty',
                    prompt: '请输入业务名称'
                },
                {
                    type: 'regExp[/^.{0,20}$/]',
                    prompt: '限制20字'
                }]
            },
            //业务编号
            businessCode: {
                identifier: 'businessCode',
                rules: [{
                    type: 'empty',
                    prompt: '请输入业务编号'
                },
                {
                    type:'regExp[/^[A-Z0-9]{2,2}$/]',
                    prompt: '两位大写字母或数字，不可重复'
                }]
            },
            //服务分类
            serviceClassifiId: {
                identifier: 'serviceClassifiId',
                rules: [{
                    type: 'empty',
                    prompt: '请选择服务分类'
                }]
            },
            //业务类型
            businessTypeId: {
                identifier: 'businessTypeId',
                rules: [{
                    type: 'empty',
                    prompt: '请输入业务类型'
                }]
            }
        }
    });
    
};